import {
    getDayData,
    getMonthData,
    getYearData
} from './request';

import {
    formatChsDate,
    mapForChsDate
} from '@/libs/utils';

export default async (store, field, date) => {
    console.log('field', field)
    let data = null;
    console.log('field', field)
    switch (field) {
        case 'day':
            data = await getDayData(date);
            break;
        case 'month':
            data = await getMonthData(date);
            break;
        case 'year':
            data = await getYearData(date);
            break;
    }

    //判断接口返回的错误码是否为0（0：请求成功）
    if (data.error_code !== 0) {
        store.commit('setErrorCode', data.error_code);
        return;
    }

    let res = null; //初始化数据
    //根据field回显对应的数据
    switch (field) {
        case 'day':
            res = data.result.data;
            res.date = formatChsDate(res.date, 'day'); 
            res['year-month'] = formatChsDate(res['year-month'], 'month');
            break;
        case 'month':
            res = data.result.data.holiday_array;
            res = mapForChsDate(res, 'festival');
            break;
        case 'year':
            res = data.result.data.holiday_list;
            res = mapForChsDate(res, 'startday');
    }

    store.commit('setData', {
        field,
        data: res
    })
}